package com.bank.ui;

import com.bank.service.TransactionService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransferDialog extends JDialog {
    private final JTextField fromCardField;
    private final JTextField toCardField;
    private final JTextField amountField;

    public TransferDialog(JFrame parent) {
        super(parent, "转账操作", true);
        setSize(400, 300);
        setLocationRelativeTo(parent);
        setLayout(new BorderLayout(10, 10));

        // 表单面板,收集转账信息：转出卡、转入卡、金额
        JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        formPanel.add(new JLabel("转出卡号:", SwingConstants.RIGHT));
        fromCardField = new JTextField();
        formPanel.add(fromCardField);

        formPanel.add(new JLabel("转入卡号:", SwingConstants.RIGHT));
        toCardField = new JTextField();
        formPanel.add(toCardField);

        formPanel.add(new JLabel("转账金额:", SwingConstants.RIGHT));
        amountField = new JTextField();
        formPanel.add(amountField);

        add(formPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        JButton transferButton = new JButton("确认转账");
        JButton cancelButton = new JButton("取消");

        buttonPanel.add(transferButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);

        // 转账按钮事件
        transferButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fromCard = fromCardField.getText().trim();
                String toCard = toCardField.getText().trim();
                String amountText = amountField.getText().trim();

                // 验证输入,验证输入有效性（非空、金额>0）
                if (fromCard.isEmpty() || toCard.isEmpty() || amountText.isEmpty()) {
                    JOptionPane.showMessageDialog(TransferDialog.this,
                            "请填写所有字段", "输入错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                double amount;
                try {
                    amount = Double.parseDouble(amountText);
                    if (amount <= 0) {
                        throw new NumberFormatException();
                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(TransferDialog.this,
                            "金额必须是正数", "输入错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 执行转账
                TransactionService service = new TransactionService();
                String result = service.transferFunds(fromCard, toCard, amount);

                // 显示结果
                JOptionPane.showMessageDialog(TransferDialog.this, result,
                        "转账结果", JOptionPane.INFORMATION_MESSAGE);

                // 如果转账成功，清空字段
                if (result.startsWith("转账成功")) {
                    fromCardField.setText("");
                    toCardField.setText("");
                    amountField.setText("");
                }
            }
        });

        // 取消按钮事件
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }
}
